package com.yx.yinshanjinniu.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.core.AppAction;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.CustomerUtil;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.LogUtil;
import com.yx.yinshanjinniu.utils.TimeCount;
import com.yx.yinshanjinniu.utils.ToastUtil;

public class BindWithdrawCardDialog extends MyDialog implements View.OnClickListener {
    private static final String TAG = "SignDialog";
    private View rootView;
    private AppAction mAppAction;
    private TextView tv_mobile,tv_get_code,tv_confirm;
    private TimeCount timeCount;
    private EditText et_code;
    private String bind_card_id;
    private String order_no;
    private DialogClick mDialogClick;
    private LinearLayout tip_ll;
    private Dialog mLoadingDialog;

    public BindWithdrawCardDialog(Context context, boolean isCancelAble, int gravity, String bind_card_id, String order_no, DialogClick dialogClick) {
        super(context, isCancelAble, gravity);
        this.mContext = context;
        this.bind_card_id = bind_card_id;
        this.order_no = order_no;
        this.mDialogClick = dialogClick;
        Window w = this.getWindow();
        if (w != null) {
            w.setWindowAnimations(R.style.dialogTranslateAnim);
        }
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_bind_withdraw_card, null);
        setContentView(rootView);
        findViews(rootView);

        getCode();
    }

    private void findViews(View rootView) {
        tv_mobile = rootView.findViewById(R.id.tv_mobile);
        tv_get_code = rootView.findViewById(R.id.tv_get_code);
        et_code = rootView.findViewById(R.id.et_code);
        tv_confirm = rootView.findViewById(R.id.tv_confirm);
        tip_ll = rootView.findViewById(R.id.tip_ll);
        UserInfoBean bean =  DataUtils.getUserInfo(mContext);
        String  mobile = bean.mobile;
        String unBindMobile = "验证码已发送至 ：" + mobile;
        tv_mobile.setText(unBindMobile);
        tv_get_code.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        tip_ll.setOnClickListener(this);
        mAppAction =  MyApplication.getApplication().getAppAction();
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.tv_get_code){
            if (tv_get_code.getText().toString().equals("再次获取")){
                if (mDialogClick != null){
                    mDialogClick.onClickCode();
                }
            }
            getCode();
            return;
        }
        if(view.getId() == R.id.tv_confirm){
            submitClick();
            return;
        }
        if (view.getId() == R.id.tip_ll){
            CustomerUtil.customerService(mContext);
            return;
        }
    }
    private void getCode(){
        ToastUtil.showToast(mContext,"发送成功");
        timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
        timeCount.start();
    }

    private void submitClick(){
        String smsCode = et_code.getText().toString();
        if(TextUtils.isEmpty(smsCode)) {
            ToastUtil.showToast(mContext, "请输入验证码");
            return;
        }
        LogUtil.d("订单id", "提交" + order_no);
        loading();
        mAppAction.confirmBindCard(bind_card_id, order_no, "", smsCode, "", new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                ToastUtil.showToast(mContext, "绑卡成功！");
                MyApplication.getApplication().updateUserDetail();
                ((BaseActivity)mContext).finish();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    /**
     * 展示loading
     */
    public void loading() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(mContext);
            mLoadingDialog.show();
        } else if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    /**
     * 关闭loading
     */
    public void cancelLoading() {
        if (null != mLoadingDialog && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
    }
    private void cancelClick(){
        dismiss();
    }

    public interface DialogClick{
        void onClickCode();
    };
}
